unit unfrmSortingGrid;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, DB, DBClient, Grids, Wwdbigrd, Wwdbgrid,
  ImgList, ActnList;

type
  TFieldsList = record
    FieldName,
    Caption: String;
  end;
  TfrmSortingGrid = class(TForm)
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    ListBox1: TListBox;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    cds_Ordenacao: TClientDataSet;
    cds_OrdenacaoFIELDNAME: TStringField;
    cds_OrdenacaoCAPTION: TStringField;
    cds_OrdenacaoORD: TSmallintField;
    wwDBGrid1: TwwDBGrid;
    dts_Ordenacao: TDataSource;
    ImageList1: TImageList;
    ActionList1: TActionList;
    act_Add: TAction;
    act_Del: TAction;
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure wwDBGrid1DblClick(Sender: TObject);
    procedure act_AddExecute(Sender: TObject);
    procedure act_AddUpdate(Sender: TObject);
    procedure act_DelUpdate(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
  private
    { Private declarations }
  public
    FieldsList: array of TFieldsList;
    { Public declarations }
  end;

var
  frmSortingGrid: TfrmSortingGrid;

implementation

uses Contnrs;

{$R *.dfm}

procedure TfrmSortingGrid.BitBtn1Click(Sender: TObject);
begin
  ModalResult:= mrOk;
end;

procedure TfrmSortingGrid.BitBtn2Click(Sender: TObject);
begin
  ModalResult:= mrCancel;
end;

procedure TfrmSortingGrid.FormCreate(Sender: TObject);
begin
  cds_Ordenacao.CreateDataSet;
end;

procedure TfrmSortingGrid.SpeedButton1Click(Sender: TObject);
begin
  if ListBox1.Selected[ListBox1.ItemIndex] then
  begin
    with cds_Ordenacao do
    begin
      Append;
      FieldByName('FIELDNAME').AsString:= ListBox1.Items.Strings[ListBox1.ItemIndex];
      FieldByName('CAPTION').AsString:= ListBox1.Items.Strings[ListBox1.ItemIndex];
      FieldByName('ORD').AsInteger:= 0;
      Post;
    end;
    ListBox1.Items.Delete(ListBox1.ItemIndex);
  end;
end;

procedure TfrmSortingGrid.wwDBGrid1DblClick(Sender: TObject);
begin
{  if wwDBGrid1.SelectedIndex = 1 then
  begin
    with cds_Ordenacao do
    begin
      Edit;
      if FieldByName('ORD').AsInteger = 0 then
      FieldByName('ORD').AsInteger:= 1
      else
      FieldByName('ORD').AsInteger:= 0;
      Post;
    end;
  end;  }
end;

procedure TfrmSortingGrid.act_AddExecute(Sender: TObject);
begin
  if ListBox1.Selected[ListBox1.ItemIndex] then
  begin
    with cds_Ordenacao do
    begin
      Append;
      FieldByName('FIELDNAME').AsString:= String(ListBox1.Items.Objects[ListBox1.ItemIndex]);
      FieldByName('CAPTION').AsString:= ListBox1.Items.Strings[ListBox1.ItemIndex];
      FieldByName('ORD').AsInteger:= 0;
      Post;
    end;
    ListBox1.Items.Delete(ListBox1.ItemIndex);
  end;
end;

procedure TfrmSortingGrid.act_AddUpdate(Sender: TObject);
begin
  TAction(Sender).Enabled:= ListBox1.ItemIndex <> -1;
end;

procedure TfrmSortingGrid.act_DelUpdate(Sender: TObject);
begin
  TAction(Sender).Enabled:= cds_Ordenacao.RecordCount <> 0;
  BitBtn1.Enabled:=  cds_Ordenacao.RecordCount <> 0;  
end;

procedure TfrmSortingGrid.SpeedButton2Click(Sender: TObject);
var
  x: Integer;
begin
 with cds_Ordenacao do
  Delete;

  ListBox1.Items.Clear;
  for x:= Low(FieldsList) to High(FieldsList) do
  if not cds_Ordenacao.Locate('FIELDNAME', frmSortingGrid.FieldsList[x].FieldName, []) then
  frmSortingGrid.ListBox1.Items.Add(frmSortingGrid.FieldsList[x].Caption);

end;

end.
